
/**
 * 地图状态管理器
 */
export  default class StateManager {
  /**
   *
   * @param changeEvent
   * @param defaultStatueMaker 默认功能构造器
   */
  constructor(changeEvent,defaultStatueMaker) {
    this.changeEvent=changeEvent;
    this.defaultStatueMaker=defaultStatueMaker;
    if(defaultStatueMaker){
      this.setCurFun(this.defaultStatueMaker())
    }
  }

  setCurFun(obj,notClearMap=false){

    if(obj.funId==this.name){
      this.removeCurrent();
      return;
    }
    this.removeCurrent(notClearMap,false);//停止当前功能，不重设功能
    this.fun=obj;
    this.name=obj.funId;
    if(this.changeEvent){
      this.changeEvent(this.name);
    }
    this.fun.start();
  }

  /**
   * 移除当前地图功能
   */
  removeCurrent(notClearMap=false,reset=true){
    if(this.name){
     this.fun.close(notClearMap);

      this.fun=null;
      this.name="";
      if(this.changeEvent){
        this.changeEvent("");
      }
    }
    if(this.defaultStatueMaker&&reset){
      setTimeout(res=>{
        this.setCurFun(this.defaultStatueMaker())
      },500);
    }
  }

  /**
   * 手动结束当前功能
   */
  closeByHand(){
    if(this.fun){
      this.fun.closeByHand();
    }
  }
  /**
   * 获取当前编辑要素
   */
  getCurFeatures(){
    if(this.fun){
     return  this.fun.getCurFeatures();
    }
  }

  getCurrentFunId(){
    return this.name;
  }
}
